package com.fyl.custom_spring.context.support;

import com.fyl.custom_spring.beans.factory.support.BeanDefinitionReader;
import com.fyl.custom_spring.beans.factory.support.BeanDefinitionRegistry;
import com.fyl.custom_spring.context.ApplicationContext;
import org.dom4j.DocumentException;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * Application接口的子实现类，用于立即加载
 */
public abstract class AbstractApplicationContext implements ApplicationContext {

    //声明解析器变量
    protected BeanDefinitionReader beanDefinitionReader;

    //定义存储bean的map容器
    protected Map<String, Object> singletonObjects = new HashMap<>();

    //声明配置文件路径的变量
    protected String configLocation;

    @Override
    public void refresh() throws DocumentException {
        //加载beanDefinition对象
        beanDefinitionReader.loadBeanDefinitions(configLocation);
        //初始化bean
    }

    //bean的初始化
    private void finishBeanInitialization() throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException {
        //获取注册表对象
        BeanDefinitionRegistry registry = beanDefinitionReader.getRegistry();
        //获取beanDefinition对象
        String[] beanNames = registry.getBeanDefinitionNames();
        for (String beanName : beanNames) {
            //进行bean的初始化
            getBean(beanName);
        }
    }
}
